/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Base {

    private static Connection conexao = null;
    private Statement st = null;
    private ResultSet rs = null;
    private static String nomeDriver = "com.mysql.jdbc.Driver";
    private static String localBancoDados = "";
    private static String usuario = "";
    private static String senha = "";
    private static boolean ok = false;

    public static String getLocalBancoDados() {
        return localBancoDados;
    }

    public static String getNomeDriver() {
        return nomeDriver;
    }

    public static String getUsuario() {
        return usuario;
    }

    public static boolean isOk() {
        return ok;
    }

    @SuppressWarnings("static-access")
    public Base(String local, String user, String pas) {
        this.localBancoDados = local;
        this.usuario = user;
        this.senha = pas;
        Conexao();
    }

    public Base() {
    }

    public Base(Connection con) {
        conexao = con;
    }

    public static boolean SAIR() {
        try {
            conexao.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static Connection getConexao() {
        return conexao;
    }

    @SuppressWarnings("static-access")
    private static void Conexao() {
        try {
            Class.forName(nomeDriver).newInstance();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        }

        try {
            conexao = (Connection) DriverManager.getConnection(localBancoDados, usuario, senha);
            ok = true;
        } catch (SQLException ex) {
            ok = false;
            System.err.println("ERRO ----------------------- CONEXAO ----------------------- MYSQL");
            ex.printStackTrace();
        }
    }

    public synchronized ResultSet executeQuery(String com) throws Exception {
        st = (Statement) conexao.createStatement();
        rs = (ResultSet) st.executeQuery(com);
        return rs;
    }

    public synchronized boolean execute(String com) throws Exception {
        st = (Statement) conexao.createStatement();
        boolean ret = st.execute(com);
        return ret;
    }

    public synchronized int executeUpdate(String com) throws Exception {
        st = (Statement) conexao.createStatement();
        int ret = st.executeUpdate(com);
        return ret;
    }

    public static synchronized ResultSet executeQueryS(String com) throws Exception {
        Statement st = (Statement) conexao.createStatement();
        ResultSet rs = (ResultSet) st.executeQuery(com);
        return rs;
    }

    public static synchronized boolean executeS(String com) throws Exception {
        Statement st = (Statement) conexao.createStatement();
        boolean ret = st.execute(com);
        return ret;
    }

    public static synchronized int executeUpdateS(String com) throws Exception {
        Statement st = (Statement) conexao.createStatement();
        int ret = st.executeUpdate(com);
        return ret;
    }

    public static synchronized int insertAutoIncrtement(String sql) {
        Statement stmt = null;
        ResultSet rs = null;
        try {
            stmt = (Statement) conexao.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY,
                    java.sql.ResultSet.CONCUR_UPDATABLE);

            stmt.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);

            int autoIncKeyFromApi = -1;

            rs = (ResultSet) stmt.getGeneratedKeys();

            if (rs.next()) {
                autoIncKeyFromApi = rs.getInt(1);
            } else {
                // throw an exception from here
            }

            rs.close();

            rs = null;

            return autoIncKeyFromApi;
        } catch (Exception e) {
            e.printStackTrace();
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException ex) {
                    // ignore
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException ex) {
                    // ignore
                }
            }
        }
        return -1;
    }

    public static void conexaoClasses() {
        Campus.setConnection(conexao);
        Chamado.setConnection(conexao);
        Chamado_has_Componente.setConnection(conexao);
        Chamado_has_Laboratorio.setConnection(conexao);
        Chamado_has_Maquina.setConnection(conexao);
        Chamado_has_Periferico.setConnection(conexao);
        Componente.setConnection(conexao);
        Componentes.setConnection(conexao);
        Estados.setConnection(conexao);
        Historico.setConnection(conexao);
        Laboratorio.setConnection(conexao);
        Licenca.setConnection(conexao);
        Licencas.setConnection(conexao);
        Maquina.setConnection(conexao);
        Periferico.setConnection(conexao);
        Perifericos.setConnection(conexao);
        Predio.setConnection(conexao);
        Programa.setConnection(conexao);
        Programas.setConnection(conexao);
        Propositos.setConnection(conexao);
        Registro.setConnection(conexao);
        Reserva.setConnection(conexao);
        Sala.setConnection(conexao);
        SistemaOperacional.setConnection(conexao);
        SistemasOperacionais.setConnection(conexao);
        Turnos.setConnection(conexao);
    }
}